package test;

import main.DataConnection;
import main.Its;
import main.Problem;

import org.junit.Assert;
import org.junit.Test;

/**
 * Class testing {@code DataConnection} static methods.
 * 
 * @author Dawid Nejman
 * @version 1.4 21-12-2011
 */
public class DataConnectionTester {

	@Test
	public void problemIOTest() {
		double oldPercentValue = DataConnection.getPercentOfReduction("Speed camera", "Accidents", "Total");
		DataConnection.setPercentOfReduction("Speed camera", "Accidents", "Total", 0.33);
		int newPercentValue = (int) (DataConnection.getPercentOfReduction("Speed camera", "Accidents", "Total") * 100 + 0.5);
		Assert.assertTrue(newPercentValue == 33);
		DataConnection.setPercentOfReduction("Speed camera", "Accidents", "Total", oldPercentValue);
		
		Problem problem = new Problem("TEST");
		problem.addData(2007, 10.5);
		problem.addData(2008, 11.4);
		problem.addData(2009, 12.3);
		problem.addData(2010, 13.2);
		problem.addData(2011, 14.1);
		
		DataConnection.addProblem(problem);
		Assert.assertTrue(DataConnection.problemAndSubproblemExists("TEST", "Total"));
		
		DataConnection.removeProblem(problem.getName());
		Assert.assertFalse(DataConnection.problemExists(problem.getName()));
	}

	@Test
	public void itsIOTest() {
		double[] reduction = new double[] { 1.2, 2.2, 2.3 };
		Its its = new Its("Test ITS");
		its.addProblem("Emission");
		its.addProblem("Accidents");
		its.addSubProblem("Emission", "Passenger car", reduction[0]);
		its.addSubProblem("Emission", "Heavy commercial vehicle", reduction[1]);
		its.addSubProblem("Accidents", "Total", reduction[2]);
		DataConnection.addIts(its);

		Assert.assertTrue(DataConnection.getItsID("Test ITS") > 0);
		Assert.assertTrue(reduction[0] == DataConnection.getPercentOfReduction("Test ITS", "Emission", "Passenger car"));
		Assert.assertTrue(reduction[1] == DataConnection.getPercentOfReduction("Test ITS", "Emission", "Heavy commercial vehicle"));
		Assert.assertTrue(0.0 == DataConnection.getPercentOfReduction("Test ITS", "Emission", "Total"));
		Assert.assertTrue(reduction[2] == DataConnection.getPercentOfReduction("Test ITS", "Accidents", "Total"));

		DataConnection.removeIts("Test ITS");
		Assert.assertTrue(DataConnection.getItsID("Test ITS") == -1);
	}

	@Test
	public void itsUsersTest() {
		double oldUsersValue = DataConnection.getItsUsers("Speed camera");
		DataConnection.setItsUsers("Speed camera", 0.35);
		int newPercentValue = (int) (DataConnection.getItsUsers("Speed camera") * 100 + 0.5);
		Assert.assertTrue(newPercentValue == 35);
		DataConnection.setItsUsers("Speed camera", oldUsersValue);
	}

	@Test
	public void characteristicTest() {
		double characteristic = DataConnection.getProblemCarCharacteristic("Accidents");
		Assert.assertTrue(characteristic != 0.0);
	}
}
